import {
  getAllCreditMarket,
  postUpdateCreditMarket,
  postAddCreditMarket,
  postDelCreditMarket,
} from '../../services/channel/api';


export default {
  namespace: 'loanSupermarket',

  state: {

    loading: true,

    allCreditMarketData: {
      list: [],
      pagination:{}
    },

  },

  effects: {

    *fetchGetAllCreditMarket({payload,callback}, { call, put }) {
      const response = yield call(getAllCreditMarket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            allCreditMarketData: {
              list: response.data.records,
              pagination: {
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    *fetchPostUpdateCreditMarket({payload,callback}, { call, put }) {
      const response = yield call(postUpdateCreditMarket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            postUpdateSortData: response.data
          },
        });
      }
      if (callback && response) callback(response);
    },

    *fetchPostAddCreditMarket({payload,callback}, { call, put }) {
      const response = yield call(postAddCreditMarket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            postAddCreditMarketData: response.data
          },
        });
      }
      if (callback && response) callback(response);
    },

    *fetchPostDelCreditMarket({payload,callback}, { call, put }) {
      const response = yield call(postDelCreditMarket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            postAddCreditMarketData: response.data
          },
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
